﻿//========================================================= 
/**@file LoggerCmdDriver.h
 * @brief 文件日志命令
 * 
 * @date 2016-09-29   10:30:25
 * @author Zhyioo 
 * @version 1.0
 */
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_LOGGERCMDDRIVER_H_
#define _LIBZHOUYB_LOGGERCMDDRIVER_H_
//--------------------------------------------------------- 
#include "../CommonCmdDriver.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace driver {
//--------------------------------------------------------- 
/// 文件日志命令驱动
class LoggerCmdDriver : public CommandCollection
{
protected:
    Ref<LoggerAdapter> _activelog;
    FolderLogger _folder;
    LoggerAdapter _log;
public:
    LoggerCmdDriver()
    {
        _log.Select(_folder);
        _activelog = _log;

        _RegisteCMD(LoggerCmdDriver, Clear);
        _RegisteCMD(LoggerCmdDriver, Flush);
        _RegisteCMD(LoggerCmdDriver, SetEnable);
        _RegisteIBaseDeviceCMD(LoggerCmdDriver);
        _RegisteInterfaceCMD(LoggerCmdDriver, LoggerAdapter);
    }
    virtual ~LoggerCmdDriver()
    {
        _folder.Flush();
        _log.Flush();

        _folder.Close();
        _log.Release();
    }
    /**
     * @brief 打开目录输出日志
     * @date 2016-09-29 10:40
     * 
     * @param [in] Folder : string 需要输出的文件目录
     * @param [in] Title : string [default:log] 输出的日志标签
     * @param [in] Count : size_t [default:LOG_FILE_COUNT(2)] 可输出的最大文件数目
     * @param [in] Size : size_t [default:LOG_FILE_SIZE(1M)] 可输出的最大文件大小(KB)
     */
    LC_CMD_METHOD(Open)
    {
        string dir = arg["Folder"].To<string>(".");
        string title = arg["Title"].To<string>("log");
        size_t count = arg["Count"].To<size_t>(LOG_FILE_COUNT);
        size_t size_k = arg["Size"].To<size_t>(LOG_FILE_SIZE);

        return _folder.Open(dir.c_str(), title.c_str(), count, size_k);
    }
    /// 返回日志是否打开
    LC_CMD_METHOD(IsOpen)
    {
        return _folder.IsOpen();
    }
    /// 关闭日志
    LC_CMD_METHOD(Close)
    {
        _folder.Close();
        return true;
    }
    /// 清空日志文件
    LC_CMD_METHOD(Clear)
    {
        _folder.Clear();
        return true;
    }
    /// 刷新缓冲区
    LC_CMD_METHOD(Flush)
    {
        _folder.Flush();
        _log.Flush();
        return true;
    }
    /**
     * @brief 设置是否开启日志
     * @date 2017-02-27 20:34
     * 
     * @param [in] IsEnable : bool 是否开启日志
     */
    LC_CMD_METHOD(SetEnable)
    {
        string sEnable;
        if(arg.Get("IsEnable", sEnable))
        {
            _log.IsEnable = ArgConvert::FromString<bool>(sEnable);
        }
        return true;
    }
    LC_CMD_INTERFACE_GETTER(LoggerAdapter, _activelog);
};
//--------------------------------------------------------- 
} // nemespace driver
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_LOGGERCMDDRIVER_H_
//========================================================= 